{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2024 Teclib' and contributors.
 # @copyright 2003-2014 by the INDEPNET Development Team.
 # @licence   https://www.gnu.org/licenses/gpl-3.0.html
 #
 # ---------------------------------------------------------------------
 #
 # LICENSE
 #
 # This file is part of GLPI.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 # ---------------------------------------------------------------------
 #}

{% set field = field|default('password') %}
{% if config('use_password_security') %}
   <span id="password_min_length" class="text-danger">{{ config('password_min_length') }}</span>
   {% set checks = {
      password_need_number: __('Digit'),
      password_need_letter: __('Lowercase'),
      password_need_caps: __('Uppercase'),
      password_need_symbol: __('Symbol')
   } %}
   {% if config('password_need_number') or config('password_need_letter') or config('password_need_caps') or config('password_need_symbol') %}
      {% set check_fields %}
         {% for name, label in checks|filter((v, k) => config(k)) %}
            <span id="{{ name }}" class="text-danger">{{ label }}</span>
            {% if not loop.last %}
               <span>, </span>
            {% endif %}
         {% endfor %}
      {% endset %}
      <span>{{ __('%1$s: %2$s')|format(__('Password must contains'), check_fields|raw)|raw }}</span>
   {% endif %}

   <script>
      function passwordCheck() {
          const pwd = $('#{{ field }}').val();
          const meet_min_length = pwd.length >= {{ config('password_min_length') }};
          const meet_need_number = {{ config('password_need_number') }} ? pwd.match(/[0-9]/g) : true;
          const meet_need_letter = {{ config('password_need_letter') }} ? pwd.match(/[a-z]/g) : true;
          const meet_need_caps = {{ config('password_need_caps') }} ? pwd.match(/[A-Z]/g) : true;
          const meet_need_symbol = {{ config('password_need_symbol') }} ? pwd.match(/[^a-zA-Z0-9]/g) : true;

          $('#password_min_length').toggleClass('text-danger', !meet_min_length).toggleClass('text-success', meet_min_length);
          $('#password_need_number').toggleClass('text-danger', !meet_need_number).toggleClass('text-success', meet_need_number);
          $('#password_need_letter').toggleClass('text-danger', !meet_need_letter).toggleClass('text-success', meet_need_letter);
          $('#password_need_caps').toggleClass('text-danger', !meet_need_caps).toggleClass('text-success', meet_need_caps);
          $('#password_need_symbol').toggleClass('text-danger', !meet_need_symbol).toggleClass('text-success', meet_need_symbol);
      }
   </script>
{% endif %}
